package com.qy127.javaweb.dao.impl;


import com.qy127.javaweb.dao.TypeDao;
import com.qy127.javaweb.entity.Type;
import com.qy127.javaweb.util.JDBCUtil;

import java.util.ArrayList;
import java.util.List;

public class TypeImpl implements TypeDao {
    @Override
    public List<Type> type() {
        String sql ="select * from t_type";
        List<List> lists = JDBCUtil.queryList(sql);
        if (lists.size()>0){
            List<Type> objects = new ArrayList<>();
            for (List list : lists) {
                Type type = new Type((Integer) list.get(0), (String) list.get(1));
                objects.add(type);
            }
           return objects;
        }
        return null;
    }

    @Override
    public int addtype(Integer typeid, String typename) {
        String sql ="insert into t_type values(?,?)";
        int update = JDBCUtil.update(sql,typeid,typename);

        return update;
    }

    @Override
    public int deltype(Integer typeid) {
        String sql ="delete from t_type where typeid=? ";
        int update = JDBCUtil.update(sql,typeid );
        return update;
    }

    @Override
    public List<Type> xg(Integer typeid) {
        String sql ="select * from t_type where typeid=?";
        List<List> lists = JDBCUtil.queryList(sql,typeid);
        if (lists.size()>0){
            List<Type> objects = new ArrayList<>();
            for (List list : lists) {
                Type type = new Type((Integer) list.get(0),(String) list.get(1));
                objects.add(type);
            }
            return objects;
        }
        return null;
    }

/*    @Override
    public List xg(Integer typeid) {
        String sql ="select * from t_type where typeid=?";
        List<List> lists = JDBCUtil.queryList(sql,typeid);
        if (lists.size()>0){
            List<Object> objects = new ArrayList<>();
            for (List list : lists) {
                Type type = new Type((Integer) list.get(0),(String) list.get(1));
                objects.add(type);
            }
            return objects;
        }
        return null;
    }*/

    @Override
    public int xadd(Integer typeid,String typename,Integer id) {
//        String sql ="insert into t_type values(?,?)";
           String sql ="update t_type set typeid=?,typename=? where typeid=?";
        int update = JDBCUtil.update(sql,id,typename,typeid);
        return update;
    }
}
